package com.ling.rocketmq.producer;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

import static org.springframework.util.Assert.notNull;

public class RocketmqScannerConfigurer implements
        BeanDefinitionRegistryPostProcessor {

    private String basePackage;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        if (!StringUtils.isEmpty(this.basePackage)) {
            notNull(this.basePackage, "Property 'basePackage' is required");
            ClassPathRocketmqScanner scanner = new ClassPathRocketmqScanner(registry);
            scanner.setProducerFactoryBeanClass(RocketmqProducerFactory.class);
            scanner.registerFilters();
            scanner.scan(
                    StringUtils.tokenizeToStringArray(
                            this.basePackage,
                            ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS
                    )
            );
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        //left intentionally blank wow!
    }


    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }


}
